package com.atguigu.gulimall.order.controller;

import java.util.Arrays;
import java.util.Map;

import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.entity.OrderReturnReasonEntity;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.atguigu.gulimall.order.entity.MqMessageEntity;
import com.atguigu.gulimall.order.service.MqMessageService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;


/**
 * @author taotao
 * @email sunlightcs@gmail.com
 * @date 2021-07-13 19:08:43
 */
@SuppressWarnings("all")
@RestController
@RequestMapping("order/mqmessage")
@RabbitListener(queues = {"hello-java-queue"})
public class MqMessageController {
    @Autowired
    private MqMessageService mqMessageService;

    /**
     * 接收消息
     * queue：指定收监听消息的队列，可以为多个
     * msg.getClass():org.springframework.amqp.core.Message
     * 参数可以写一下类型
     * 1、Message msg：原生消息类型详细信息，里面有消息头等信息
     * 2、T <发送的消息的类型> OrderReturnReasonEntity content
     * 3、Channel channel：数据传输都是通过通道。一个客户端只建立一条连接，channel表示的就是当前数据传输的通道
     * <p>
     * Queue:可以有很多人都来监听，但是只有一个人能收到消息，收到之后队列删除消息
     * 场景：
     * 1）订单服务启动多个,同一个消息只能有一个客户端收到，
     * 2）只有一个消息完全处理完，方法运行结束，我们才可以接收到下一个消息
     */
//    @RabbitListener(queues = {"hello-java-queue"})
    @RabbitHandler
    public void receiveMessage(Message msg, OrderReturnReasonEntity content, Channel channel) throws InterruptedException {
        System.out.println("内容===》" + content);
        byte[] body = msg.getBody();//消息体
        MessageProperties messageProperties = msg.getMessageProperties();//消息头
        System.out.println("消息处理完成" + content.getName());
        //deliveryTag：当前通道内自增的
        long deliveryTag = msg.getMessageProperties().getDeliveryTag();
        System.out.println("deliveryTag===>" + deliveryTag);//1,2,3,4...,只要没有回复服务器就一直是unacked状态
        //签收,false表示非批量签收，
        try {
            if (deliveryTag % 2 == 0) {
                //收获
                channel.basicAck(deliveryTag, false);
                System.out.println("签收了货物。。。" + deliveryTag);
            } else {
                //退货,multiple参数如果设为true，那么之前的都会退货，requeue表示是否重新入队
                //void basicNack(long deliveryTag, boolean multiple, boolean requeue)
                channel.basicNack(deliveryTag, false, false);
                //void basicReject(long deliveryTag, boolean requeue)
//                channel.basicReject();
                System.out.println("没有签收货物。。。" + deliveryTag);
            }
        } catch (Exception e) {
            //网络中断异常
        }
    }

    @RabbitHandler
    public void receiveMessage2(Message msg, OrderEntity content, Channel channel) throws InterruptedException {
        System.out.println("接收到消息" + content);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = mqMessageService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{messageId}")
    public R info(@PathVariable("messageId") String messageId) {
        MqMessageEntity mqMessage = mqMessageService.getById(messageId);

        return R.ok().put("mqMessage", mqMessage);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MqMessageEntity mqMessage) {
        mqMessageService.save(mqMessage);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MqMessageEntity mqMessage) {
        mqMessageService.updateById(mqMessage);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody String[] messageIds) {
        mqMessageService.removeByIds(Arrays.asList(messageIds));

        return R.ok();
    }

}
